<?php

/**
 * Example Component
 *
 * An example hook component for demonstrating hook system.
 *
 * @category Component
 * @package  Croogo
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class CountriesComponent extends Component {

        /**
         * Other components used by this component
         *
         * @var array
         * @access public
         */
        public $components = array(
            'Croogo'
        );

        /**
         * Menus for layout
         *
         * @var string
         * @access public
         */
        public $countriesForLayout = array();
        public $citiesForLayout = array();
        public $limit_countries = array();

        /**
         * initialize
         *
         * @param Controller $controller instance of controller
         */
        public function initialize(Controller $controller) {
                $this->controller = $controller;
                $this->Country = ClassRegistry::init('Countries.Country');
                $this->City = ClassRegistry::init('Countries.City');
                $this->limit_countries = json_decode(Configure::read('Countries.country'));
        }

        /**
         * Startup
         *
         * @param object $controller instance of controller
         * @return void
         */
        public function startup(Controller $controller) {
                if (!isset($controller->request->params['admin']) && !isset($controller->request->params['requested'])) {
                        //$this->countries();
                        //$this->cities();
                } else {
                        
                }
        }

        /**
         * beforeRender
         *
         * @param object $controller instance of controller
         * @return void
         */
        public function beforeRender(Controller $controller) {
                $controller->set('countries_for_layout', $this->countriesForLayout);
                $controller->set('cities_for_layout', $this->citiesForLayout);
        }

        /**
         * Country
         *
         * Menus will be available in this variable in views: $countries_for_layout
         *
         * @return void
         */
        public function countries() {
                $countries = array();
                $countries = $this->Country->find('all', array('conditions' => array('Country.id' => $this->limit_countries)));
                $this->countriesForLayout = $countries;
        }

        /**
         * City
         *
         * Nodes will be available in this variable in views: $cities_for_layout
         *
         * @return void
         */
        public function cities() {
                $cities = $this->controller->Blocks->blocksData['cities'];
                $_nodeOptions = array(
                    'find' => 'all',
                    'conditions' => array(
                        'City.status' => 1,
                        'City.country_id' => $this->limit_countries
                    ),
                    'order' => 'City.weight ASC',
                    'limit' => 5,
                );

                foreach ($cities as $alias => $options) {
                        $options = Hash::merge($_nodeOptions, $options);
                        $options['limit'] = str_replace('"', '', $options['limit']);
                        $city = $this->City->find($options['find'], array(
                            'conditions' => $options['conditions'],
                            'order' => $options['order'],
                            'limit' => $options['limit'],
                            'cache' => array(
                                'prefix' => 'croogo_cities_' . $alias . '_',
                                'config' => 'croogo_cities',
                            ),
                                ));
                        $this->citiesForLayout[$alias] = $city;
                }
        }

}

?>